package spring.cloud.tasks.common.utils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Schedules {

    /**
     * 分配
     */
    public static int[] allot(int totalCount, int num) {
        int[] ints = new int[num];
        int allHaveCount = totalCount / num;
        int mod = totalCount % num;
        for (int i = 1; i <= ints.length; i++) {
            if (i <= mod) {
                ints[i - 1] = allHaveCount + 1;
            } else {
                ints[i - 1] = allHaveCount;
            }
        }
        return ints;
    }

    public static Map<String, String> allot(List<String> taskItemList, List<String> processorIdList) {
        Map<String, String> taskItemToProcessorIdMap = new HashMap<String, String>();
        for (int i = 0; i <= taskItemList.size() - 1; i++) {
            String taskItem = taskItemList.get(i);
            String processorId = processorIdList.get(i % processorIdList.size());
            taskItemToProcessorIdMap.put(taskItem, processorId);
        }
        return taskItemToProcessorIdMap;
    }

    public static Thread getThreadByThreadName(String threadName) {
        for (Thread thread : Thread.getAllStackTraces().keySet()) {
            if (thread.getName().equals(threadName)) {
                return thread;
            }
        }
        return null;
    }

}
